package com.woke.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.bean.FootItem;
import com.jakewharton.rxbinding2.view.RxView;
import com.zhongcai.online.BR;
import com.zhongcai.online.R;


import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewProvider;

/**
 * 作者 ： 暗网
 * 创建时间：2017/3/29
 * 备注：
 */
public class FootItemViewBinder extends ItemViewProvider<FootItem, FootItemViewBinder.ViewHolder> {

	private OnItemClickListener mItemClickListener;

	public FootItemViewBinder(OnItemClickListener listener) {
		mItemClickListener = listener;
	}

	@NonNull
	@Override
	protected ViewHolder onCreateViewHolder(
			@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
		ViewDataBinding binding = DataBindingUtil.inflate (inflater, R.layout.item_foot_item, parent, false);
		ViewHolder holder = new ViewHolder (binding.getRoot ());
		holder.setBinding (binding);
		return holder;
	}

	@Override
	protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final FootItem footItem) {
		holder.getBinding ().setVariable (BR.footItem, footItem);
		RxView.clicks (holder.footBtn)
				.throttleFirst (2, TimeUnit.SECONDS)
				.subscribe (new Consumer<Object> () {
					@Override
					public void accept(Object o) throws Exception {
						if (mItemClickListener != null) {
							mItemClickListener.OnItemClick (holder.footBtn, footItem, getPosition (holder));
						}
					}
				});

	}

	static class ViewHolder extends RecyclerView.ViewHolder {
		private ViewDataBinding binding;
		private Button footBtn;

		ViewHolder(View itemView) {
			super (itemView);
			footBtn = (Button) itemView.findViewById (R.id.foot_btn);
		}

		public ViewDataBinding getBinding() {
			return binding;
		}

		public void setBinding(ViewDataBinding binding) {
			this.binding = binding;
		}
	}
}
